﻿using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDream.admin
{
    public partial class acti : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        void Bindat()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.UserDBs
                           where k.kichhoat == false && k.UserIDTuvan > 0
                           select new
                           {
                               k.ID,
                               k.UserName,
                               k.FirtName,
                               k.Lastname,
                               k.phone,
                               k.ngaythamgia,
                               k.nguoigioithieu,
                               k.TuvanName,
                               k.gioitinh,
                               k.email

                           };
                grvpResult.DataSource = list;
                grvpResult.DataBind();
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }

        protected void grvpResult_Load(object sender, EventArgs e)
        {
            Bindat();
        }

        void kich_hoat(bool type)
        {
            string listUserID = "0";
            GridViewDataColumn column1 = grvpResult.Columns["check"] as GridViewDataColumn;
            for (int i = 0; i < grvpResult.VisibleRowCount; i++)
            {
                ASPxCheckBox chk = (ASPxCheckBox)grvpResult.FindRowCellTemplateControl(i, column1, "chkCheck");
                HiddenField hdfUserID = (HiddenField)grvpResult.FindRowCellTemplateControl(i, column1, "hdfUserID");
                if (type == true)
                    listUserID = listUserID + "," + hdfUserID.Value;
                else if (chk.Checked == true && type == false)
                    listUserID = listUserID + "," + hdfUserID.Value;


            }
            if (listUserID != "0")
            {
                DataClasses1DataContext dx = new DataClasses1DataContext();
                dx.Connection.Open();
                try
                {
                    float muc1 = 0, muc2 = 0, muc3 = 0;
                    var list = from k in dx.GetTable<config_mucHoahong>()
                               select k;
                    foreach (config_mucHoahong cf in list)
                    {
                        if (cf.ID.Equals("Muc1"))
                            muc1 = (float)cf.Value;
                        if (cf.ID.Equals("Muc2"))
                            muc2 = (float)cf.Value;
                        if (cf.ID.Equals("Muc3"))
                        {
                            muc3 = (float)cf.Value;
                            break;
                        }
                    }

                    //dx.sp_Acti_Employee(listUserID.Substring(2), muc1, muc2, muc3);
                   // dx.SubmitChanges();
                }
                catch (Exception) { }
                finally { dx.Connection.Close(); }
            }
        }

        protected void btnTatca_Click(object sender, EventArgs e)
        {
            kich_hoat(true);
            Bindat();
            lbThongbao.Text = "Quá trình xử lý hoàn tất";
        }

        protected void btnDachon_Click(object sender, EventArgs e)
        {
            kich_hoat(false);
            Bindat();
            lbThongbao.Text = "Quá trình xử lý hoàn tất";
        }
    }
}